<template>
	<view class="" :class="[config.style]">
		<!-- 顶部固定部分 -->
		<view class="category">
			<scroll-view class="scroll-view-main" scroll-x="true" scroll-left="10">
				<view class="item" :class="categoryId == 0 ? 'active':''" @click="changeCategory(0)">
					<text>全部</text>
				</view>
				<view class="item" :class="categoryId == item.id ? 'active':''" v-for="(item,index) in category"  :key="index" @click="changeCategory(item.id)">
					<text>{{item.title}}</text>
				</view>
			</scroll-view>
		</view>
		
		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" top="82" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list">
					<view class="item" v-for="(item,index) in list" :key="index" @click="goToDetailLink(item.id)">
						<view class="content">
							<view class="info">
								<view class="title text-ellipsis-2">{{item.title}}</view>
								<view class="cont">
									<view class="exp-info">
										<view class="category">
											<text>{{item.category.title}}</text>
										</view>
										<view class="info">
											<view class="item view">
												<uni-icons type="eye" size="10" color="#999999"></uni-icons> 
												<text class="num">{{item.handling_view}}</text>
											</view>
											<view class="item support">
												<uni-icons type="star" size="10" color="#999999"></uni-icons> 
												<text class="num">{{item.support}}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
							<view class="cover">
								<image mode="widthFix" :src="item.cover_400" @error="imageError" class="image"></image>
							</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import Search from '../../diys/Search/Search.vue' //搜索组件


	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			Search,
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				page: 1, //当前页码
				category: [], // 文章分类数据
				list: [], //列表数据
				categoryId: 0, //分类当前ID
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
			}
		},
		onLoad(option) {
			//获取url参数
			this.urlParams = option
		},
		created() {
			this.getCategoryTree()
		},
		computed: {
			...mapState(['config'])
		},
		methods: {

			/**
			 * 获取文章分类数据
			 */
			getCategoryTree: function(){
				let that = this
				let params = this.urlParams
				let data = {}
					data.action = 'category'
				//获取数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/article'
					data.m = 'muu_classroom'
					//#endif
					//#ifdef H5
					url = 'entry/site/article'
					data.m = 'muu_classroom_plugin_article'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.category = res.data.data
						}
					}
				})
			},
			/**
			 * 跳转至分类页
			 * @param {Object} category_id
			 */
			changeCategory: function(category_id){
				console.log(category_id)
				this.categoryId = category_id
				this.page = 1
				this.list = []
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			
			//上拉加载的回调
			upCallback: function() {
				let that = this
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
					data.category_id = this.categoryId
				//获取数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/article'
					data.m = 'muu_classroom'
					//#endif
					//#ifdef H5
					url = 'entry/site/article'
					data.m = 'muu_classroom_plugin_article'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				this.page = 1
				this.list = []
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
					data.category_id = this.categoryId
				//获取首页数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/article'
					data.m = 'muu_classroom'
					//#endif
					//#ifdef H5
					url = 'entry/site/article'
					data.m = 'muu_classroom_plugin_article'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			
			/**
			 * 进入文章详情页
			 * @param {Object} id
			 */
			goToDetailLink: function(id){
				let urlParams = 'id='+id
				let url = 'detail?' + urlParams
				//console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*文章分类部分*/
	.category {
		margin: 0 10px;
		.scroll-view-main {
			white-space: nowrap;
			width: 100%;
			overflow: hidden;
			.item {
				position: relative;
				font-size: 28rpx;
				display: inline-block;
				margin: 12rpx 20rpx;
				line-height: 48rpx;
				color: #666;
				&:first-child {
					margin-left: 0;
				}
			}
			.active {
				font-size: 30rpx;
				font-weight: 700;
				color: #000;
				z-index: 1;
				&:after {
					content: '';
					position: absolute;
					background-color: #03b8cf;
					display: block;
					z-index: 2;
					top: 44rpx;
					left: 0;
					right: 0;
					width: 60%;
					height: 16rpx;
					margin: 0 auto;
					-webkit-transform-origin: 50% 100%;
					-ms-transform-origin: 50% 100%;
					transform-origin: 50% 100%;
					-webkit-transform: scaleY(0.33);
					-ms-transform: scaleY(0.33);
					transform: scaleY(0.33);
				}
			}
		}
	}

	/*竖排*/
	.list {
		background-color: #fff;
		.item {
			display: block;

			.content {
				padding: 10rpx;
				display: flex;
				.cover {
					width: 240rpx;
					border-radius: 10rpx;
					overflow: hidden;
					.image {
						width: 100%;
						display: block;
					}
				}
				.info {
					position: relative;
					margin-right: 20rpx;
					flex: 1;
					
					.title {
						font-size: 30rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.cont {
						position: absolute;
						bottom: 0;
						left: 0;
						right: 0;
						.exp-info {
							display: flex;
							flex-wrap: wrap;
							margin-top: 10rpx;
							color: #999;
							font-size: 24rpx;
							.category {
								flex: 1;
							}
							.info {
								flex: 1;
								text-align: right;
								.item {
									display: inline;
									.num {
										margin: 0 10rpx;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	/*页面风格设计*/
	.Blue {
		
	}
</style>
